//
//  ProductInfoController.m
//  jang
//
//  Created by lee han on 09/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "ProductInfoController.h"
#import "CommonView.h"
#import "network.h"
#import "UIImageView+WebCache.h"
#import "ProductStandardController.h"

@interface ProductInfoController ()

@end

@implementation ProductInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem.backBarButtonItem setTitle:@""];
    // Do any additional setup after loading the view.
    _mName.text = [_mBaseInfo objectForKey:@"pname"];
    _mPrice.text = [NSString stringWithFormat:@"%@ /%@", [_mBaseInfo objectForKey:@"price"], [_mBaseInfo objectForKey:@"unit"]];
    NSString* urlstr = [URLIMAGE_PRE stringByAppendingString:[_mBaseInfo objectForKey:@"path"]];
    [_mImage sd_setImageWithURL:[NSURL URLWithString:urlstr] placeholderImage:[UIImage imageNamed:@"wenhao.png"]];
    
    NSInteger count = 0;
    for (NSDictionary* info in _mReseverInfo) {
        count += [[info objectForKey:@"displaycount"] integerValue];
    }
    
    _mCount.text = [NSString stringWithFormat:@"%ld", count];
    
    _mWebView.delegate = self;
    [_mWebView.scrollView setScrollEnabled:NO];
    
    NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                       "<head> \n"
                       "<style type=\"text/css\"> \n"
                       "body {font-size:15px;}\n"
                       "</style> \n"
                       "</head> \n"
                       "<body>"
                       "<script type='text/javascript'>"
                       "window.onload = function(){\n"
                       "var $img = document.getElementsByTagName('img');\n"
                       "for(var p in  $img){\n"
                       " $img[p].style.width = '100%%';\n"
                       "$img[p].style.height ='auto'\n"
                       "}\n"
                       "}"
                       "</script>%@"
                       "</body>"
                       "</html>",[_mBaseInfo objectForKey:@"content"]];
    
    [_mWebView loadHTMLString: htmls baseURL:[NSURL URLWithString:URLIMAGE_PRE]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onClickOrder:(id)sender {
    ProductStandardController* standard = [[ProductStandardController alloc] initWithNibName:@"ProductStandardController" bundle:nil];
    
    standard.mTitle = [_mBaseInfo objectForKey:@"pname"];
    standard.mStandardData = _mReseverInfo;
    standard.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:standard animated:YES completion:^{
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [CommonView addLoadingView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [CommonView removeLoadingView];
    
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
    

    CGRect r = _mWebView.frame;
    r.size.height = height;
    _mWebView.frame = r;
    [_mWebConstraint setConstant:height];
    CGSize size = _mScrollView.contentSize;
    size.height += height;
    [_mScrollView setContentSize:size];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [CommonView removeLoadingView];
    [CommonView addAlertView:self Text:@"信息加载错误"];
}
//end UIWebViewDelegate
@end
